package servlet;

import java.io.PrintWriter;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Participation;
import model.Study;
import model.User;
import DAO.DepositDao;
import DAO.ParticipationDao;
import DAO.StudyDao;
import DAO.UserDao;
import DAO.UserDetailDao;
import servlet.Util;

public class StudyNewAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	
		Study study = new Study();
		StudyDao studyDao = new StudyDao();
		ParticipationDao participationDao = new ParticipationDao();
		Participation participation = new Participation();
		HttpSession session=request.getSession();
		UserDetailDao userDetailDao = new UserDetailDao();
		ActionForward forward=null;

		int userIdCheck = session.getAttribute("userid") != null
				&& session.getAttribute("userid") != "" ? Integer
				.parseInt((String)session.getAttribute("userid")) : 0;
		if (userIdCheck < 1) {
			session.invalidate();
			Util.ShowError(response, "로그인해주세요");
		}
	
		
		
		int category = request.getParameter("category") != null && request.getParameter("category") 
				!= "" ? Integer.parseInt(request.getParameter("category")) : -1;
		

		int userId = session.getAttribute("userid") != null && session.getAttribute("userid") != "" ? Integer.parseInt((String) session.getAttribute("userid")) : 0; 
				
		int memberLimit =request.getParameter("memberlimit")!= null && request.getParameter("memberlimit")
				!= ""? Integer.parseInt(request.getParameter("memberlimit")) : 0;
		String title = request.getParameter("title")!= null && request.getParameter("title") 
				!= ""? request.getParameter("title") : null;		
		String content = request.getParameter("content")!= null && request.getParameter("content") 
				!= ""? request.getParameter("content") : null;		
		int deposit = request.getParameter("deposit")!= null && request.getParameter("deposit") 
				!= ""? Integer.parseInt(request.getParameter("deposit")) : 0;
		int fine = request.getParameter("fine")!= null&& request.getParameter("fine") 
				!= "" ? Integer.parseInt(request.getParameter("fine")) : 0;
		Date expiredTime = request.getParameter("expiredtime")!= null&& request.getParameter("expiredTime") 
				!= "" ? Date.valueOf(request.getParameter("expiredtime")) : null;
		
		Timestamp expiredTimeStamp = new Timestamp(expiredTime.getTime());
				
				
		System.out.println("expiredTime " + expiredTime );		
		if(category < 0 || userId<1 || memberLimit<1 || title==null || content==null || deposit <1 || fine<1 ||expiredTime==null)  {
			//error return
			Util.ShowError(response, "값을 올바로 입력해주세요");
			
			return null;
		}
		
		title=Util.toUTF8(title);
		content=Util.toUTF8(content);

		study.setCid(category);
		study.setUid(userId);
		study.setMemberLimit(memberLimit);
		study.setTitle(title);
		study.setContent(content);
		study.setDid(deposit);
		study.setFid(fine);
		study.setExpiredTime(expiredTimeStamp);
		
		UserDao userDao = new UserDao();
		DepositDao depositDao = new DepositDao();
		
		if(userDao.getUserInfo(userId).getPoint()>=depositDao.getDepositValue(deposit)){
			studyDao.insertStudyInfo(study);
			userDetailDao.updateOngoingStudy(userId);
			
		}
		else {
			Util.ShowError(response, "잔액이 부족합니다.");
			return forward;
		}
	
		
		ArrayList<Integer> studyIdList = new ArrayList<Integer>();
		studyIdList = userDao.getUserJoinedStudyInfo(userId);
		
		ArrayList<Study> studyList = new ArrayList<Study>();
		
		for(int i=0; i<studyIdList.size(); i++)
		{
			studyList.add(studyDao.getStudyInfo(studyIdList.get(i)));
		}
		
		session.setAttribute("studyList", studyList);	
		
	
		// go to details studyId;
		Util.ShowSuccess(response, "스터디가 생성되었습니다.", "./questudy.jsp");

		
		return forward;
	}
}
